package com.manxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manxi.dto.TrendsDTO;
import com.manxi.dto.TrendsPageQueryDTO;
import com.manxi.entity.Trends;
import com.manxi.mapper.TrendsMapper;
import com.manxi.result.PageResult;
import com.manxi.service.TrendsService;
import com.manxi.vo.TrendsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrendsServiceImpl implements TrendsService {
    @Autowired
    private TrendsMapper trendsMapper;


    /**
     * 新增动态
     *
     * @param trendsDTO
     * @return
     */
    public void save(TrendsDTO trendsDTO) {
        Trends trends = new Trends();
        BeanUtils.copyProperties(trendsDTO,trends);
        trendsMapper.insert(trends);
    }

    /**
     * 动态分页查询
     *
     * @param trendsPageQueryDTO
     * @return
     */
    public PageResult pageQuery(TrendsPageQueryDTO trendsPageQueryDTO) {
        PageHelper.startPage(trendsPageQueryDTO.getPage(), trendsPageQueryDTO.getPageSize());
       Page<TrendsVO> page = trendsMapper.pageQuery(trendsPageQueryDTO);

       return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询动态
     *
     * @param id
     * @return
     */
    public TrendsVO getById(Long id) {
       Trends trends = trendsMapper.getById(id);
        TrendsVO trendsVO = new TrendsVO();
        BeanUtils.copyProperties(trends,trendsVO);
        return trendsVO;
    }

    /**
     * 批量删除动态和删除单个动态
     *
     * @param ids
     * @return
     */
    public void deleteBatch(List<Long> ids) {
        //可以一次删除一条动态，也可以批量删除多条动态
        for (Long id : ids) {
            trendsMapper.deleteById(id);
        }

    }


    /**
     * 更改动态信息
     *
     * @param trendsDTO
     * @return
     */
    public void update(TrendsDTO trendsDTO) {
        Trends trends = new Trends();
        BeanUtils.copyProperties(trendsDTO,trends);
        trendsMapper.update(trends);
    }

}
